package hczc.longcai.com.hccrowdfunding.home.fourth_mine.activities;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.activity.BaseActivity;

public class RechargeActivity extends BaseActivity implements View.OnClickListener {

    private static final int STATE_ZFB = 0;
    private static final int STATE_WX = 1;

    private static final int TARGET_ZFB = 0;
    private static final int TARGET_WX = 1;

    private int target = -1;
    private int state = -1;

    private RelativeLayout zfb_layout;
    private ImageView zfb;
    private RelativeLayout wx_layout;
    private ImageView wx;
    private EditText czje;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge);

        initViews();
    }

    private void initViews() {
        ((TextView) findViewById(R.id.head_title)).setText("充值");
        findViewById(R.id.head_back).setOnClickListener(this);

        zfb_layout = ((RelativeLayout) findViewById(R.id.rechagre_zfb_layout));
        zfb_layout.setOnClickListener(this);
        zfb = ((ImageView) findViewById(R.id.recharge_zfb));

        wx_layout = ((RelativeLayout) findViewById(R.id.rechagre_wx_layout));
        wx_layout.setOnClickListener(this);
        wx = ((ImageView) findViewById(R.id.recharge_wx));

        czje = ((EditText) findViewById(R.id.recharge_czje));

        findViewById(R.id.recharge_button).setOnClickListener(this);
    }

    private boolean isEmpty() {
        boolean flag = true;
        String money = czje.getText().toString().trim();
        if (TextUtils.isEmpty(money)) {
            setFocus(czje);
            flag = false;
            return flag;
        }


        return flag;
    }

    /**
     * 设置EditText的焦点
     *
     * @param et 要设置的EditText
     */
    private void setFocus(EditText et) {
        et.setFocusable(true);
        et.setFocusableInTouchMode(true);
        et.requestFocus();
        et.findFocus();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.head_back:
                finish();
                break;
            case R.id.rechagre_zfb_layout:
                target = TARGET_ZFB;
                choosePay(target);
                break;
            case R.id.rechagre_wx_layout:
                target = TARGET_WX;
                choosePay(target);
                break;
            case R.id.recharge_button:
                if (!zfb.isSelected() && !wx.isSelected()) {
                    Toast.makeText(RechargeActivity.this, "请选择支付方式", Toast.LENGTH_SHORT).show();
                } else if(!isEmpty()){
                    Toast.makeText(RechargeActivity.this, "请输入充值金额", Toast.LENGTH_SHORT).show();
                } else
                    Toast.makeText(RechargeActivity.this, "充值成功", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private void choosePay(int target) {
        if (target == -1) {
            zfb.setSelected(false);
            wx.setSelected(false);
        } else if (target == TARGET_ZFB) {
            zfb.setSelected(true);
            wx.setSelected(false);
            state = STATE_ZFB;
        } else {
            wx.setSelected(true);
            zfb.setSelected(false);
            state = STATE_WX;
        }
    }


}
